package com.theta.gateway.facade.web.controller;



import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.theta.gateway.biz.dto.request.RouteCfgQuery;
import com.theta.gateway.biz.dto.request.RouteCfgStatusChangeRequest;
import com.theta.gateway.biz.dto.request.RouteSaveRequest;
import com.theta.gateway.biz.dto.response.RouteCfgVO;
import com.theta.gateway.biz.service.RouteCfgService;
import com.theta.gateway.infra.common.base.PageResult;
import com.theta.gateway.infra.common.base.Result;
import com.theta.gateway.infra.common.utils.GatewayRequestUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;




/**
 * <p>
 * 动态路由配置表，服务表 前端控制器
 * </p>
 *
 * @author theta
 * @since 2022-04-14
 */

@Slf4j
@Api(tags = "网关路由信息管理")
@RestController
@RequestMapping("saas/gateway/router/cfg")
public class RouteCfgController {
	
	@Autowired
	private RouteCfgService routeCfgService;
	
	
	
	/**
	 * 分页查询路由配置信息
	 * @param query
	 * @return
	 */
	@ApiOperation(value = "分页查询路由信息")
	@PostMapping("page")
	public Mono<Result<PageResult<RouteCfgVO>>> page(@RequestBody RouteCfgQuery query){
		return Mono.just(Result.success(this.routeCfgService.queryRoutePage(query)));
		
	}
	
	@ApiOperation(value = "保存路由信息")
	@SuppressWarnings("unchecked")
	@PostMapping("save")
	public Result<Void> save(@Valid @RequestBody RouteSaveRequest request){
		this.routeCfgService.saveRouteCfg(request);
		return Result.success();
	}
	
	@ApiOperation(value = "查看路由信息")
	@GetMapping("detail")
	public Result<RouteCfgVO> detail(ServerHttpRequest request ,@RequestParam("id")Long id){
		log.info("test header{}",GatewayRequestUtil.getHeader(request, "testtoken"));
		return Result.success(this.routeCfgService.queryDetailById(id));
	}
	
	@ApiOperation(value = "删除路由信息")
	@SuppressWarnings("unchecked")
	@PostMapping("delete")
	public Result<Void> delete(@RequestParam("id")Long id){
		this.routeCfgService.deleteRouteCfg(id);
		return Result.success();
	}
	
	@ApiOperation(value = "路由启用/禁用")
	@SuppressWarnings("unchecked")
	@PostMapping("status/change")
	public Result<Void> statusChange(@Valid @RequestBody RouteCfgStatusChangeRequest request){
		this.routeCfgService.changeRouteCfgStatus(request);
		return Result.success();
	}
	
	@ApiOperation(value = "重新加载路由")
	@SuppressWarnings("unchecked")
	@PostMapping("refresh")
	public Result<Object> refresh(){
		this.routeCfgService.refreshAllRoutes();
		return Result.success();
	}

}

